﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POSSystem.DAL;
using System.Web;

namespace POSSystem.BLL
{
    public class Banners : _Banners
    {
        public static void AddNewBanner(string fileName, string desc, string url, int page)
        {
            try
            {
                Banners banner = new Banners();
                banner.AddNew();

                banner.PageID = page;
                banner.Deleted = false;
                banner.Description = desc;
                banner.FileName = fileName;
                banner.URL = url;
                banner.LastModifiedDate = DateTime.UtcNow.AddHours(SystemVariables.SysVars.TimeZone);

                banner.Save();
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Banners.cs", "AddNewBanner", ex.StackTrace);
            }
        }

        public static Banners GetAll()
        {
            Banners banner = new Banners();
            banner.Where.Deleted.Value = false;
            banner.Query.Load();

            return banner;
        }

        public static Banners GetByPageID(int pageID)
        {
            Banners banner = new Banners();
            banner.Where.Deleted.Value = false;
            banner.Where.PageID.Value = pageID;
            banner.Query.Load();

            return banner;
        }

        public static void Delete(int id)
        {
            try
            {
                Banners banner = new Banners();
                banner.LoadByPrimaryKey(id);

                if (banner.RowCount > 0)
                {
                    HttpContext context = HttpContext.Current;
                    string file = context.Server.MapPath(banner.s_FileName);
                    try
                    {
                        if (System.IO.File.Exists(file))
                            System.IO.File.Delete(file);
                    }
                    catch (Exception ex1) { }
                    banner.Deleted = true;
                    banner.LastModifiedDate = DateTime.UtcNow.AddHours(SystemVariables.SysVars.TimeZone);

                    banner.Save();
                }
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Banners.cs", "AddNewBanner", ex.StackTrace);
            }
        }
    }
}
